import {view, BondViewGroup, handler} from "adajs";
import NewsService from "./state.js";
import Bar from "../../../controls/bar";
import ActivityDetail from "../../../controls/detail/activity";
import {getQueryInfo} from "./../../../lib/helper";
import Messagebox from "./../../../controls/messagebox";
import Form from "./../../../controls/form";
import Text from "./../../../controls/form/text";
import Loading from "./../../../controls/loading";
import Reading from "../../../controls/reading";

@view({
    className: "news",
    template: "./template.html",
    style: "./style.scss",
    dataset: {
        service: NewsService
    }
})
class News extends BondViewGroup {
    tags() {
        return {
            bar: Bar,
            detail: ActivityDetail
        }
    }

    onready() {
        let r = getQueryInfo();
        if (r.id) {
            this.commit("load", r.id);
        }
    }

    @handler("bar-add")
    add() {
        this.addChild(Messagebox, {
            name: "box",
            parameter: {
                title: "添加评论",
                content: [
                    {
                        type: Form, option: {
                            fields: [
                                {name: "comment", type: Text, required: true}
                            ]
                        }
                    }
                ],
                btns: [
                    {name: "关闭", action: "close"},
                    {name: "评论", action: "comment"}
                ]
            }
        })
    }

    @handler("comment")
    comment(e) {
        let form = e.target.getChildAt(0);
        form.check().then(a => {
            if (a) {
                form.getValue().then(({comment}) => {
                    this.addChild(Loading).then(loading => {
                        this.context.request.post("/api/lgb/app/lgb/branchexchangemsg/insert", {
                            themeId: this.getCurrentState().id,
                            msgcontent: comment
                        }).then((a) => {
                            if (a.code === '1') {
                                loading.showSuccess("评论成功");
                                loading.close().then(() => {
                                    this.getChildrenByType(ActivityDetail)[0].refreshList();
                                    this.getChildByName("box").close();
                                });
                            } else {
                                loading.showSuccess(a.msg);
                                loading.close();
                            }
                        }, () => {
                            loading.showError('服务异常');
                            loading.close();
                        })
                    });
                });
            }
        });
    }

    @handler("bar-listen")
    listen() {
        let info = this.getChildrenByType(ActivityDetail)[0].getCurrentState().newsInfo;
        this.addChild(Reading, {
            parameter: {
                info: info.theme + "。" + info.content
            }
        });
    }
}

export default News;